package com.shuqi.activity.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.activity.LoginActivity.LoginHandler;
import com.shuqi.application.ShuqiApplication;
import com.shuqi.controller.R;
import com.shuqi.database.model.UserInfo;
import com.shuqi.model.bean.AccountLoginHistoryInfo;

public class LoginAccountAdapter extends BaseAdapter {

    private List<AccountLoginHistoryInfo> list = new ArrayList<AccountLoginHistoryInfo>();

    private LoginHandler handler;

    public LoginAccountAdapter(List<AccountLoginHistoryInfo> accountList, LoginHandler h) {
        list = accountList;
        handler = h;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = newView();
        }
        bindView(convertView, list.get(position), position);
        return convertView;
    }

    private void bindView(View convertView, final AccountLoginHistoryInfo model, int position) {
        final Holder cache = (Holder) convertView.getTag();
        String account = model.getLoginAccount();
        cache.account.setText(account);
        cache.del.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (handler != null) {
                    Message msg = new Message();
                    msg.what = LoginHandler.LOGINHANDLER_DEL_ACCOUNT;
                    msg.obj = model;
                    handler.handleMessage(msg);
                }
            }
        });

        cache.account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (handler != null) {
                    Message msg = new Message();
                    msg.what = LoginHandler.LOGINHANDLER_LOGIN_ACCOUNT;
                    msg.obj = model;
                    handler.handleMessage(msg);
                }
            }
        });
    }

    private View newView() {
        LayoutInflater inflater =
                (LayoutInflater) ShuqiApplication.getContext().getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.login_auto_complete_item, null, false);

        final Holder holder = new Holder();

        holder.del = (ImageView) view.findViewById(R.id.login_account_del_img);
        holder.account = (TextView) view.findViewById(R.id.login_account_text);

        view.setTag(holder);

        return view;

    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    static class Holder {
        ImageView del;
        TextView account;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public void setList(List<AccountLoginHistoryInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }
}
